<?php
App::uses('Component', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class MailComponent extends Component{
    
    public $mail;
    public $email_sender = null;
    public $email_contact = null;
    public $email_method;
    public $config;

    private function bootstrapMail(){
        App::import('model', 'Setting');
        $ob = new Setting();
        $this->mail = new CakeEmail();
        $email_method = $ob->getSettingValue("email_method");
        $email_sender = $ob->getSettingValue("email_sender");
        $email_contact = $ob->getSettingValue("email_contact");
        if($email_method !== false && $email_sender !== false && $email_contact !== false){
            $this->email_contact = $email_contact;
            $this->email_sender = $email_sender;
            $this->email_method = $email_method;
            if($email_method == "smtp"){
                $smtp_host = $ob->getSettingValue("smtp_host");
                $smtp_port = $ob->getSettingValue("smtp_port");
                $smtp_username = $ob->getSettingValue("smtp_username");
                $smtp_password = $ob->getSettingValue("smtp_password");
                if($smtp_host !== false && $smtp_port !== false && $smtp_username !== false && $smtp_password !== false ){
                    $this->config = array(
                        'host' => $smtp_host,
                        'port' => $smtp_port,
                        'username' => $smtp_username,
                        'password' => $smtp_password,
                        'transport' => 'Smtp',
                        'emailFormat' => 'html'
                    );
                    $this->mail->config($this->config);
                    $this->mail->delivery = 'smtp';
                    return true;
                }
                return false;
            }
            return true;
        }
        return false;
    }
    
    /**
     *
     * @param type $toMail
     * @param type $subject
     * @param type $body
     * @return type 
     */
    public  function sendMail($toMail,$subject,$body){
        if($this->bootstrapMail()){
            $this->mail->from(array($this->email_contact => $this->email_sender));
            $this->mail->to($toMail);
            $this->mail->subject($subject);
            $this->mail->send($body);
            return true;
        }
        return false;
    }
    
    /**
     * 
     */
    public function getTempalte($template="default",$data = array()){
        $content = "don't has email template";
        $filename =  ROOT . DS . APP_DIR . DS . 'Mail' . DS.$template.".html";
        if(file_exists($filename)){
            $content = file_get_contents($filename);
            foreach($data as $key => $value){
                $content = str_replace("%%".$key."%%", $value, $content);
            }
        }
        return $content;
        
    }
    
    /**
     * 
     * @return type
     */
    public function getEmailContact(){
        App::import('model', 'Setting');
        $ob = new Setting();
        $this->email_contact = $ob->getSettingValue("email_contact");
        return $this->email_contact;
    }
    
}
